package com.hdfs.client.service;

import org.apache.hadoop.fs.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

public interface HDFSService {
    /**
     * 在HDFS创建文件夹
     * @param path
     * @return
     * @throws Exception
     */
    boolean mkdir(String path) throws Exception;

    /**
     * 判断HDFS文件是否存在
     * @param path
     * @return
     * @throws Exception
     */
    boolean existFile(String path) throws Exception;

    /**
     * 读取HDFS目录信息
     * @param path
     * @return
     * @throws Exception
     */
    List<Map<String, Object>> readPathInfo(String path) throws Exception;

    /**
     * HDFS创建文件
     * @param path
     * @param file
     * @throws Exception
     */
    void createFile(String path, MultipartFile file) throws Exception;

    /**
     * 读取HDFS文件内容
     * @param path
     * @return
     * @throws Exception
     */
    String readFile(String path) throws Exception;

    /**
     * 读取HDFS文件列表
     * @param path
     * @return
     * @throws Exception
     */
    List<Map<String, String>> listFile(String path) throws Exception;

    /**
     * HDFS重命名文件
     * @param oldName
     * @param newName
     * @return
     * @throws Exception
     */
    boolean renameFile(String oldName, String newName) throws Exception;

    /**
     * 删除HDFS文件
     * @param path
     * @return
     * @throws Exception
     */
    boolean deleteFile(String path) throws Exception;

    /**
     * 上传HDFS文件
     * @param path
     * @param uploadPath
     * @throws Exception
     */
    void uploadFile(String path, String uploadPath) throws Exception;

    /**
     * 下载HDFS文件
     * @param path
     * @param downloadPath
     * @throws Exception
     */
    void downloadFile(String path, String downloadPath) throws Exception;

    /**
     * HDFS文件复制
     * @param sourcePath
     * @param targetPath
     * @throws Exception
     */
    void copyFile(String sourcePath, String targetPath) throws Exception;

    /**
     * 打开HDFS上的文件并返回byte数组
     * @param path
     * @return
     * @throws Exception
     */
    byte[] openFileToBytes(String path) throws Exception;

    /**
     * 打开HDFS上的文件并返回java对象
     * @param path
     * @return
     * @throws Exception
     */
    <T extends Object> T openFileToObject(String path, Class<T> clazz) throws Exception;

    /**
     * 获取某个文件在HDFS的集群位置
     * @param path
     * @return
     * @throws Exception
     */
    BlockLocation[] getFileBlockLocations(String path) throws Exception;
}
